﻿#include "FrozenFighter.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Enums.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    FrozenFighter::FrozenFighter(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool FrozenFighter::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 3 || y > 3)
        {
            return false;
        }

        return (x == 0) || (y == 0) || (x == y);
    }

    void FrozenFighter::Attack()
    {
        ShockTime = 0;

        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        if (!ranged && getEnvir()->Random->Next(3) > 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }
            getTarget()->Attacked(this, damage, DefenceType::ACAgility);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->Type = 1;
            Broadcast(tempVar2);
            LineAttack(3);

            PoisonTarget(getTarget(), 7, 5, PoisonType::Dazed, 1000);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void FrozenFighter::LineAttack(int distance)
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        for (int i = 1; i <= distance; i++)
        {
            Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), i);

            if (target == getTarget()->getCurrentLocation())
            {
                getTarget()->Attacked(this, damage, DefenceType::ACAgility);
            }
            else
            {
                if (!getCurrentMap()->ValidPoint(target))
                {
                    continue;
                }

                Cell *cell = getCurrentMap()->GetCell(target);
                if (cell->Objects.empty())
                {
                    continue;
                }

                for (int o = 0; o < cell->Objects.size(); o++)
                {
                    MapObject *ob = cell->Objects[o];
                    if (ob->getRace() == ObjectType::Monster || ob->getRace() == ObjectType::Player)
                    {
                        if (!ob->IsAttackTarget(this))
                        {
                            continue;
                        }
                        ob->Attacked(this, damage, DefenceType::ACAgility);
                    }
                    else
                    {
                        continue;
                    }

                    break;
                }
            }
        }
    }

    void FrozenFighter::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            if (getTarget()->Dead)
            {
                FindTarget();
            }
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }
}
